/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package inet.controller;

import inet.dao.NotesDAO;
import inet.dao.UserDAO;
import inet.entity.Branch;
import inet.entity.Company;
import inet.entity.Department;
import inet.entity.Employee;
import inet.entity.Notes;
import inet.entity.Position;
import inet.entity.UserActive;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author ÂuNgọcThái
 */
@ManagedBean
@SessionScoped
public class UserController extends NotesController {

    /**
     * Creates a new instance of UserController
     */
    private boolean isLogin;
    private Employee emp = null;
    public static  Employee empLogin = null;
    private UserDAO usaerDAO = null;
    private List<Employee> lstEmp = null;
    private Company com = null;
    private Branch br = null;
    private Department dep = null;
    private Position pos = null;
    private UserActive userActive = null;
    private String password2=null;
    public UserController() {
        emp = new Employee();
        empLogin = new Employee();
        usaerDAO = new UserDAO();
        lstEmp = new LinkedList<Employee>();
        com = new Company();
        br = new Branch();
        dep = new Department();
        pos = new Position();
        userActive = new UserActive();
        password2 = new String();
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public UserActive getUserActive() {
        return userActive;
    }

    public void setUserActive(UserActive userActive) {
        this.userActive = userActive;
    }

    public Company getCom() {
        return com;
    }

    public void setCom(Company com) {
        this.com = com;
    }

    public Branch getBr() {
        return br;
    }

    public void setBr(Branch br) {
        this.br = br;
    }

    public Department getDep() {
        return dep;
    }

    public void setDep(Department dep) {
        this.dep = dep;
    }

    public Position getPos() {
        return pos;
    }

    public void setPos(Position pos) {
        this.pos = pos;
    }

    public boolean isIsLogin() {
        return isLogin;
    }

    public void setIsLogin(boolean isLogin) {
        this.isLogin = isLogin;
    }

    public Employee getEmp() {
        return emp;
    }

    public void setEmp(Employee emp) {
        this.emp = emp;
    }

    public Employee getEmpLogin() {
        return empLogin;
    }

    public void setEmpLogin(Employee empLogin) {
        this.empLogin = empLogin;
    }

    public UserDAO getUsaerDAO() {
        return usaerDAO;
    }

    public void setUsaerDAO(UserDAO usaerDAO) {
        this.usaerDAO = usaerDAO;
    }

    public List<Employee> getLstEmp() {
        return lstEmp;
    }

    public void setLstEmp(List<Employee> lstEmp) {
        this.lstEmp = lstEmp;
    }

    public void checkAcc() {

        FacesContext facesContext = FacesContext.getCurrentInstance();
        empLogin = usaerDAO.login(empLogin.getAcc_name(), empLogin.getPassword());
        if (empLogin == null) {
            facesContext.addMessage("frmLogin", new FacesMessage("Tên đăng nhập hoặc mật khẩu không đúng!"));
            empLogin = new Employee();
            isLogin = false;
//            return "";
        } else {
            if (empLogin.getStatus() == 0) {
                try {
                    isLogin = true;
                    showPosition();
                    checkRole();
                     if (userActive.isIsManager()) {
                        showNotes(empLogin.getEmp_id());
                    }
                    facesContext.getCurrentInstance().getExternalContext().redirect("user/");      
//                    return "";
                } catch (IOException ex) {
                    Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
                    isLogin = false;
//                    return "";
                }
            } else {
                facesContext.addMessage("frmLogin", new FacesMessage("Tài khoản đã bị khóa!"));
                isLogin = false;
                empLogin = new Employee();
//                return "";
            }
        }
    }

    public void doLogout() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        isLogin = false;
        empLogin = new Employee();
        try {
            facesContext.getCurrentInstance().getExternalContext().redirect("");
        } catch (IOException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String updateByEmp() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        usaerDAO = new UserDAO();
        //usaerDAO.updateByEmp(empLogin);
        if (usaerDAO.updateByEmp(empLogin)) {
            facesContext.addMessage("frmUpdate", new FacesMessage("Cập nhật thành công"));
            return "";
        } else {
            facesContext.addMessage("frmUpdate", new FacesMessage("Cập nhật không thành công"));
            return "";
        }
    }

    public void changePassword() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        usaerDAO = new UserDAO();
        if (usaerDAO.checkPassword(empLogin.getEmp_id(), emp.getPassword()) == 1) {
            if (usaerDAO.changePassword(empLogin)) {
                facesContext.addMessage("frmChange:ms", new FacesMessage("Thay đổi thành công"));
            } else {
                facesContext.addMessage("frmChange:ms", new FacesMessage("Thay đổi không thành công"));
            }
        } else {
            facesContext.addMessage("frmChange:passold", new FacesMessage("Mật khẩu cũ không chính xác"));
        }
    }
    public void showPosition() {
        usaerDAO = new UserDAO();
        if(empLogin!=null){
        pos = usaerDAO.showPosionEmp(empLogin.getEmp_id());
        br = usaerDAO.showBranchEmp(pos.getBranch_id());
        dep = usaerDAO.showDepEmp(pos.getDep_id());
        }else{
            empLogin = new Employee();
        }
    }

    public void checkRole() {
        if (empLogin.getRoll() == 0) {
            //admin
            userActive.setIsAdmin(true);
            userActive.setIsManager(true);
            userActive.setIsEmployee(true);
        } else if (empLogin.getRoll() == 1) {

            //truong phong
            userActive.setIsAdmin(false);
            userActive.setIsManager(true);
            userActive.setIsEmployee(true);
        } else {
            //nhan vien
            userActive.setIsAdmin(false);
            userActive.setIsManager(false);
            userActive.setIsEmployee(true);
        }
    }
    public void checkPassword() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        usaerDAO = new UserDAO();
        if (usaerDAO.checkPassword(empLogin.getEmp_id(), emp.getPassword()) != 1) {
             facesContext.addMessage("frmChange:passold", new FacesMessage("Mật khẩu cũ không chính xác"));
        } 
    }
     public void checkSamePassword(String a) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        
        if (!emp.getPassword().equals(a)) {
            facesContext.addMessage("frmChange:password2", new FacesMessage("Mật khẩu không giống nhau"));
        } 
    }
    
}
